using System;
using System.Collections.Generic;
using System.Text;
using TelnetExpect;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;

namespace TelnetConsole
{
    class Expect1
    {
        string m_hostName, m_username, m_password;
        Expector m_xp;

        public Expect1(string hostName, string username, string password)
        {
            m_hostName = hostName;
            m_username = username;
            m_password = password;
        }

        public void Run()
        {
            TelnetStream telnet = Connect();
            // Don't want to see our keystrokes echoed back!
            telnet.SetRemoteMode(TelnetOption.Echo, false);
            m_xp = new Expector(telnet);
            //
            m_xp.Expect("login:");
            m_xp.SendLine(m_username);
            m_xp.Expect("password:");
            m_xp.SendLine(m_password);
            m_xp.Expect(">");
            m_xp.SendLine("prompt $g$g$g"); // >>>
            m_xp.Expect(">>>");
            m_xp.SendLine("dir g* && sleep 10 && dir *");
            m_xp.Expect(">>>");
            //m_xp.SendLine("exit");
            //Cause a EoF--
            m_xp.Expect("two green bottles");
        }

        public void Run0()
        {
            TelnetStream telnet = Connect();
            // Don't want to see our keystrokes echoed back!
            telnet.SetRemoteMode(TelnetOption.Echo, false);
            m_xp = new Expector(telnet);
            //
            m_xp.Expect("login:");
            m_xp.SendLine(m_username);
            m_xp.Expect("password:");
            m_xp.SendLine(m_password);
            m_xp.Expect(">");
            m_xp.SendLine("dir");
            m_xp.Expect(">");
            m_xp.SendLine("exit");
            //Cause a EoF-- m_xp.Expect("two green bottles");
        }

        /// <summary>
        /// Check whether the specified Service is running.
        /// </summary>
        /// -
        /// <remarks>
        /// <para>If the service is running then this method will return without
        /// error, if not then an exception will result.
        /// </para>
        /// </remarks>
        /// -
        /// <param name="ntserviceDescr">The name of the service as listed by
        /// "net start", e.g. "Windows Time", or "Task Scheduler", etc.
        /// </param>
        public void Run1_CheckServiceRunning(string serviceDescr)
        {
            TelnetStream telnet = Connect();
            // Don't want to see our keystrokes echoed back!
            telnet.SetRemoteMode(TelnetOption.Echo, false);
            m_xp = new Expector(telnet);
            //
            m_xp.Expect("login:");
            m_xp.SendLine(m_username);
            m_xp.Expect("password:");
            m_xp.SendLine(m_password);
            m_xp.Expect(">");
            m_xp.SendLine("net start");
            m_xp.SendLine("sleep 10 & exit");
            // If the service is running, it will be listed by "net start" and
            // the following line will match it.  If not we'll read to EoF and
            // an error will be produced.  (EndOfStreamException if the exit
            // command works, or IOException if timeout occurs).
            m_xp.Expect("   " + serviceDescr);
            // If we got here then the service is running!
        }

        private TelnetStream Connect()
        {
            TcpClient cli = new TcpClient(m_hostName, TelnetStream.DefaultPort);
            Stream peer = cli.GetStream();
            TelnetStream telnet = new TelnetStream(peer);
            return telnet;
        }

    }
}
